
# https://www.baidu.com/s?wd=%E5%91%A8%E6%9D%B0%E4%BC%A6


# 需求 获取 https://www.baidu.com/s?wd=周杰伦
# 的网页源码

import urllib.request
import urllib.parse


url = 'https://www.baidu.com/s?wd='

# 请求对象的定制为了解决反爬的第一种手段
headers = {
    #"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    #"Accept-Encoding": "gzip, deflate, br, zstd",
    #"Accept-Language": "zh-CN,zh;q=0.9",
    #"Cache-Control": "max-age=0",
    #"Connection": "keep-alive",
    "Cookie": "BIDUPSID=2E47CE0E865DA08208594E951DDA03FD; PSTM=1755478323; BAIDUID=2E47CE0E865DA082B5D43ACA96F2A2A7:FG=1; BD_UPN=12314753; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_WISE_SIDS=62325_63148_63324_63948_64049_64174_64245_64247_64259_64261_64317_64330_64360_64365_64361_64364_64395_64417_64430_64436_64450_64457_64473_64484_64514_64087_64562_64564_64574; H_PS_PSSID=62325_63148_63324_63948_64020_64049_64174_64245_64247_64259_64261_64317_64330_64360_64365_64361_64364_64395_64417_64430_64436_64450_64457_64473_64484_64514_64087_64562_64564_64574; BAIDUID_BFESS=2E47CE0E865DA082B5D43ACA96F2A2A7:FG=1; BA_HECTOR=ah8k8laka10k8h21850k012lal80851ka4uqj24; ZFY=G1Kjn6:BcwicPboe:BoYzVpG5AqTPFR5w3ICOlF:BzfYbw:C; delPer=0; BD_CK_SAM=1; PSINO=1; H_PS_645EC=f000o64me%2FI6%2ByA1PEbxfmTZUgGuQM%2B4Xpb6j2qxLa9Dz%2Bod54VaBfZvjJ8; baikeVisitId=27be42b3-43ec-4990-81f1-a69d454934e2",
    #"Host": "www.baidu.com",
    #"Sec-Ch-Ua": '"Google Chrome";v="137", "Chromium";v="137", "Not/A)Brand";v="24"',
    #"Sec-Ch-Ua-Mobile": "?0",
    #"Sec-Ch-Ua-Platform": '"Windows"',
    #"Sec-Fetch-Dest": "document",
    #"Sec-Fetch-Mode": "navigate",
    #"Sec-Fetch-Site": "none",
    #"Sec-Fetch-User": "?1",
    #"Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"
}

# 将周杰伦三个字变成unicode编码的格式
# 我们需要依赖于urllib.parse
name = urllib.parse.quote('周杰伦')

url = url + name

# 请求对象的定制
request = urllib.request.Request(url=url,headers=headers)

# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(request)

# 获取响应的内容
content = response.read().decode('utf-8')

# 打印数据
print(content)


